package cn.com.agameofthrones.AGoTLCGCards.Fragment;

import java.util.HashMap;
import java.util.Map;

import cn.com.agameofthrones.AGoTLCGCards.AGoTContentProvider;
import cn.com.agameofthrones.AGoTLCGCards.R;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.ViewGroup;

public class SingleCardActivity extends FragmentActivity {
	private ViewPager mCardsPager;
	private static Cursor mIDCursor;
	//private FragmentStatePagerAdapter mPagerAdapter;
	 @Override
	 protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	     
	        
	        setContentView(R.layout.single_card_pager);
	        mCardsPager = (ViewPager)findViewById(R.id.cards_pager);
	        mCardsPager.setId(R.id.cards_pager);
	        
	        
	        mCardsPager.setAdapter(new CardsAdapter(getSupportFragmentManager()));
	        
	        
	        String where_clause=getIntent().getStringExtra(
            		"cn.com.agameofthrones.AGoTLCGCards.search_sql");
	        mIDCursor=this.managedQuery(
            		AGoTContentProvider.SEARCH_CARDS_URI, new String[]{"_id"}, where_clause, null, null);
	        int current_item=getIntent().getIntExtra(
            		"cn.com.agameofthrones.AGoTLCGCards.current_position",0);
	        mCardsPager.setCurrentItem(current_item);
	 }
	 

	 public ViewPager getViewPager(){
		 return mCardsPager;
	 }
	 @Override
	 public boolean onTouchEvent (MotionEvent event){
		 System.out.println(event);//never reach here.
		 return false;
	 }
	 public static class CardsAdapter extends FragmentStatePagerAdapter{
		//this map will keep 'active Fragment'
		private Map<Integer, SingleCardFragment> mPageReferenceMap = new HashMap<Integer, SingleCardFragment>();
		public CardsAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int position) {
			// TODO Auto-generated method stub
			mIDCursor.moveToPosition(position);
			SingleCardFragment scf=SingleCardFragment.newInstance(mIDCursor.getString(0));
			mPageReferenceMap.put(position, scf);
			return scf;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			super.destroyItem(container, position, object);
			mPageReferenceMap.remove(position);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mIDCursor.getCount();
		}
		
		@Override
		public Object instantiateItem (ViewGroup container, int position){
			return super.instantiateItem(container, position);
		}
		
		public SingleCardFragment getFragment(int position){
			return mPageReferenceMap.get(position);
		}
	   }

}
